import { createRouter, createWebHistory } from 'vue-router'
import { Notify } from "vant";
import store from '../store';
const Home = () =>
    import ('../views/home/Home');
const Category = () =>
    import ('../views/category/Category');
const Detail = () =>
    import ('../views/detail/Detail');
const User = () =>
    import ('../views/profile/Profile');
const ShopCart = () =>
    import ('../views/shopcart/ShopCart');
const Register = () =>
    import ('../views/profile/Register');
const Login = () =>
    import ('../views/profile/Login');

const routes = [{
        path: '/',
        name: 'DefaultHome',
        component: Home,
        meta: {
            title: '图书商城'
        }
    },
    {
        path: '/home',
        name: 'Home',
        component: Home,
        meta: {
            title: '图书商城'
        }
    },
    {
        path: '/category',
        name: 'Category',
        component: Category,
        meta: {
            title: '分类'
        }
    },
    {
        path: '/detail',
        name: 'Detail',
        component: Detail
    },
    {
        path: '/profile',
        name: 'User',
        component: User,
        meta: {
            title: '个人中心',
            isAuthRequired: true
        }
    },
    {
        path: '/shopcart',
        name: 'ShopCart',
        component: ShopCart,
        meta: {
            title: '购物车',
            isAuthRequired: true
        }
    },
    {
        path: '/register',
        name: 'Register',
        component: Register,
        meta: {
            title: '新用户注册'
        }
    },
    {
        path: '/login',
        name: 'Login',
        component: Login,
        meta: {
            title: '登录'
        }
    },
]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})

router.beforeEach((to, from, next) => {
    // 如果没有登录，在这里调到login
    if (to.meta.isAuthRequired && store.state.user.isLogin === false) {
        Notify('请先登录');
        return next('/login')
    } else {
        next();
    }

    document.title = to.meta.title;
})

export default router